package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


@Service
@Slf4j
public class SetmealServiceimpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }




    /**
     * 修改套餐状态
     * @param status 状态信息
     * @param id  套餐ID
     */
    @Override
    public void setStatus(Long status, Long id) {
        // 基于ID 修改状态信息
        //先获得状态 在判断后修改
        SetmealVO setmealVO = setmealMapper.selectByIdSetmeal(id);
        Setmeal setmeal= new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
            // 是 1 就改为 0
            setmeal.setStatus(StatusConstant.DISABLE);
            setmealMapper.updateSetmeal(setmeal);
        }else {
            // 是 0 就改为 1
            setmeal.setStatus(StatusConstant.ENABLE);
            setmealMapper.updateSetmeal(setmeal);
        }

    }

    /**
     * 修改套餐信息
     * @param setmealDTO 套餐信息
     */
    @Transactional(rollbackFor = Exception.class)//多表操作
    @Override
    public void setSetmeal(SetmealDTO setmealDTO) {
        //基于获得信息 修改数据库内容
        Setmeal setmeal= new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        // 修改 套餐菜品关系 先删除
        Long id = setmealDTO.getId();
        setmealDishMapper.deleteSetmeal(id);
        //删除后 添加新内容进数据库
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.insertDishSetmeal(setmealDishes);


    }

    /**
     * 回显套餐
     * @param id 套餐ID
     * @return 回显 套餐和 套餐菜品的关联信息
     */
    @Override
    public SetmealVO selectSetmeal(Long id) {
        //获得套餐信息
       SetmealVO setmealVO = setmealMapper.selectByIdSetmeal(id);
       // 获得套餐关联菜品信息
        setmealVO.setSetmealDishes(setmealDishMapper.selectByIdSetmealDish(id));
        //log.info("[菜品回显]setmealVO：{}",setmealVO);

        return setmealVO;
    }

    /**
     * 批量删除 套餐
     * @param ids  套餐ID
     */
    @Override
    public void deleteSetmeal(List<Long> ids) {
        // 起售状态不能删除
        List<Long> status = setmealMapper.selectByIdsStatus(ids);




        for (Long statu : status) {
            if(statu.equals(StatusConstant.ENABLE.longValue())) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //删除套餐信息
        setmealMapper.deleteSetmeal(ids);
        //删除套餐菜品关系
        setmealDishMapper.deleteSetmealDish(ids);
    }

    /**
     * 分页查询
     * @param pageQueryDTO 每页条数 每页展示条数
     * @return 返回条件查询后的数据
     */
    @Override
    public PageResult pageSelectSetmeal(SetmealPageQueryDTO pageQueryDTO) {
        //分页查询
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
       Page<SetmealVO> page = (Page<SetmealVO>) setmealMapper.selectPageSetmeal(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐分类
     * @param setmealDTO 套餐信息
     */
    @Transactional(rollbackFor = Exception.class)  //多表关联
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //添加套餐信息
//        log.info("[套餐信息]setmeal：{}",setmeal);
        setmealMapper.addSetmeal(setmeal);
//        log.info("[套餐菜品ID]setmeal：{}",setmeal.getId());
        Long id = setmeal.getId();
        //添加套餐 菜品关系
        List<SetmealDish> setmealDish = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDish) {
            dish.setSetmealId(id);
        }
//        log.info("[套餐菜品信息]setmealDish：{}",setmealDish);
        setmealDishMapper.insertDishSetmeal(setmealDish);

    }
}
